package com.foreknow.service.impl;

import com.foreknow.dao.RuleDao;
import com.foreknow.dao.RuleMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.RuleExample;
import com.foreknow.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class RuleServiceImpl implements RuleService {
    @Autowired
    private RuleMapper ruleMapper;
    @Autowired
    private RuleDao ruleDao;

    @Override
    public List<Rule> queryRuleForAll() throws Exception {
        return null;
    }

    //不可用
    @Override
    public List<Rule> queryRuleByCondition(Integer deptId) throws Exception {
//        RuleExample ruleExample = new RuleExample();
//        RuleExample.Criteria criteria = ruleExample.createCriteria();
//        criteria.andDeptidEqualTo(deptId);

//        return ruleMapper.selectByExample(ruleExample);
//不可用
        return ruleDao.selectForRuleListPage(deptId, 0, 100);
    }

    @Override
    public int updateRule(Rule rule) throws Exception {
        return ruleMapper.updateByPrimaryKeySelective(rule);
    }

    @Override
    public int addRule(Rule rule) throws Exception {
        return ruleMapper.insert(rule);
    }

    @Override
    public int deleteRule(List<Integer> id) throws Exception {
        RuleExample example = new RuleExample();
        RuleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(id);

        return ruleMapper.deleteByExample(example);
    }

    @Override
    public List<Rule> selectForRuleListPage(Integer deptId, Integer pageSize, Integer currentPage) throws Exception {
        Integer start = (currentPage - 1) * pageSize;
        Integer offset = pageSize;

        return ruleDao.selectForRuleListPage(deptId, start, offset);
    }

    @Override
    public int countOfDept(Integer deptId) throws Exception {
        RuleExample example = new RuleExample();
        RuleExample.Criteria criteria = example.createCriteria();
        criteria.andDeptidEqualTo(deptId);

        return ruleMapper.countByExample(example);

    }
}
